<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
 	<title>管理平台</title>
	<script src="js/jquery-2.0.1.js"></script>
	<script src="js/bootstrap.min.js"></script>
	<link href="css/bootstrap.min.css" rel="stylesheet">
	<link href="icheck-1.x/skins/square/green.css" rel="stylesheet">
	<script src="icheck-1.x/icheck.js"></script>

	<link rel="stylesheet" href="artdialog/skins/blue.css" type="text/css" />
	<script src="artdialog/artDialog.source.js" type="text/javascript" charset="utf-8"></script>
	<script src="artdialog/iframeTools.source.js" type="text/javascript" charset="utf-8"></script>

	<script src="layer/layer.js" type="text/javascript" charset="utf-8"></script>
  	<style>
		#github-webview {
			height: 100%;
			width: 100%;
			position: fixed;
			overflow: hidden;
		}

		#github-webview #github-iframe {
			width: 100%;
			height: 100%;
			overflow: hidden;
			border: none;
		}
	</style>
</head>
<body>
  <div id="github-webview" class="pure-u-1">
		<iframe id="github-iframe" name="mainframe" nwfaketop></iframe>
	</div>
	<!-- 模态框（Modal） -->
	<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×
					</button>
					<h4 class="modal-title" id="myModalLabel">
						系统设置
					</h4>
				</div>
				<div class="modal-body">
					<form class="form-horizontal" role="form">
						<div class="form-group">
							<label for="firstname" class="col-sm-3 control-label">服务器地址：</label>
							<div class="col-sm-9">
								<input type="text" class="form-control" id="remortroot" placeholder="请输入服务器地址">
							</div>
						</div>
						<div class="form-group">
							<label for="lastname" class="col-sm-3 control-label">端口号：</label>
							<div class="col-sm-9">
								<input type="text" class="form-control" id="remortport" placeholder="请输入端口号">
							</div>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<table width="100%">
						<tr>
							<td>
								<lable style="float:left;font-size:9pt;margin-top:3px">时钟锁:<span id="eam_hid"></span></lable>
							</td>
							<td rowspan="2" style="text-align:right">
								<button type="button" class="btn btn-default" data-dismiss="modal">
									取消
								</button>
								<button type="button" id="initsetbtn" class="btn btn-primary">
									确定
								</button>
							</td>
						</tr>
						<tr>
							<td>
								<lable style="float:left;font-size:9pt;margin-top:3px">版本号:1.6 ID:202108020002</lable>
							</td>
						</tr>
					</table>



				</div>
			</div><!-- /.modal-content -->
		</div><!-- /.modal-dialog -->
	</div><!-- /.modal -->
  <script>
		
//检测开关(true false)
const checkIfLock = window.electronAPI.checkIfLock;
const checkArm = window.electronAPI.captureUKey;
const closeWindow = window.electronAPI.closeWindow;
// 打开设置
window.electronAPI.onAction(({ type, data }) => {
  switch(type) {
    case 'openSettings':
      showSetting()
      break;
  }
})

// 显示设置
function showSetting () {
	try {
		var remortroot = localStorage.target_host;
		var remortport = localStorage.target_port;
		if (remortroot != null) {
			$("#remortroot").val(remortroot);
		}
		if (remortport != null) {
			$("#remortport").val(remortport);
		}
		$('#myModal').modal('show');
	} catch (err) {
		$('#myModal').modal('show');
	}
}

// ===========================================
var timeout;
var lockState = false;
$(function () {
	checkIfLock().then(res => {
		lockState = res;
	})
  //初始化设置
  var remortroot = localStorage.target_host;//服务器IP地址
  var remortport = localStorage.target_port;//端口号

  if (remortroot == null || remortroot == "" || remortroot == "undefined"
    || remortroot == null || remortroot == "" || remortroot == "undefined") {
    var err = "首次登录，请填写网络配置";
    showConfirmMsg(err, function (r) {
      if (r) {
        $('#myModal').modal('show');
      }
    });
  } else {
    loadLoginPage(remortroot, remortport);
  }
});

// 校验端口
function isPort(str) {
  var parten = /^(\d)+$/g;
  if (parten.test(str) && parseInt(str) <= 65535 && parseInt(str) >= 0) {
    return true;
  } else {
    return false;
  }
}
// 校验IP
function isIP(strIP) {
  var re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g
  if (re.test(strIP)) {
    if (RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256) return true;
  }
  return false;
}
// 设置IP和端口
function sendUrlPort(remortroot, remortport) {
  var err = "";
  if (isIP(remortroot)) {
  } else {
    err += "服务器地址无效,";
  }
  if (isPort(remortport)) {
  } else {
    err += "端口号无效,";
  }
  if (err != "" && err.length > 0) {
    err = err.substring(0, err.length - 1);
    showConfirmMsg(err, function (r) {
      if (r) {
        $('#myModal').modal('show');
      }
    });
  } else {
    layer.msg('正在加载登录页面，请稍候。。。', {
      icon: 16,
      shade: 0.01,
      time: 5000000,
      shadeClose: false
    });
    var dpmHid = $("#eam_hid").text();
    var ifUd = "";
    if (false == lockState) {//false代表 u盾登录 需要验证uid
      ifUd = "0x123456"
    }
    var url = "https://" + remortroot + ":" + remortport;
    $.ajax({
      url: url,
      type: 'GET',
      timeout: 100000,
      datatype: "json",
      complete: function (response, textStatus) {
        //启动u盾检测
        timeout = setInterval("testUd()", 3000);

        layer.closeAll();
        if (response.status == 200) {
          localStorage.target_host = remortroot;
          localStorage.target_port = remortport;
          $('#github-iframe').attr('src', url);
        } else if (textStatus == 'timeout') {
          showConfirmMsg('未能成功连接系统(超时)，请检查网络配置或联系管理员!', function (r) {
            if (r) {
              $('#myModal').modal('show');
            }
          });
        } else {
          showConfirmMsg('未能成功连接系统，请检查网络配置或联系管理员!', function (r) {
            if (r) {
              $('#myModal').modal('show');
            }
          });
        }
      }
    });
  }
}

//启动时：加载登录页，并判断u端是否存在
function loadLoginPage(remortroot, remortport) {
  checkArm().then(res => {
		if (true == lockState) {
			if (false == res.flag) {
				$("#eam_hid").text("");
			}
			sendUrlPort(remortroot, remortport);
		} else {
			if (true == res.flag) {
				$("#eam_hid").text(res.randomNum);
				sendUrlPort(remortroot, remortport);
			} else {
				showLongErrorMsg("未插入U盾");
			}
		}
	})
  
}
//启动后 监测u盾是否插入，未插入则退出系统
function testUd() {
	checkArm().then(res => {
		if (true == lockState) {
			if (false == res) {
				$("#eam_hid").text("");
			}
		} else {
			if (false == res) {
				clearInterval(timeout);
				showLongErrorMsg("未插入U盾");
			}
		}
	})
}
function showConfirmMsg(msg, callBack) {
  art.dialog({
    id: 'confirmId',
    title: '系统提示',
    content: msg,
    icon: 'warning',
    background: '#000000',
    opacity: 0.1,
    lock: true,
    button: [{
      name: '确定',
      callback: function () {
        callBack(true);
      },
      focus: true
    }]
  });
}
//错误提示
function showErrorMsg(msg) {
  top.art.dialog({
    id: 'errorId',
    title: '系统提示',
    content: msg,
    icon: 'error',
    time: 5,
    background: '#000',
    opacity: 0.1,
    lock: true,
    okVal: '关闭',
    ok: true
  });
}
function showLongErrorMsg(msg) {
  top.art.dialog({
    id: 'errorId',
    title: '5秒后自动关闭客户端...',
    content: msg,
    icon: 'error',
    time: 5,
    background: '#000',
    opacity: 0.1,
    lock: true,
    cancelVal: '关闭',
    cancel: function () {
      closeWindow();
    },
    close: function () {
      closeWindow();
    }
  });
}


//弹出框事件 
$("#initsetbtn").click(function () {
	var remortroot = $("#remortroot").val();
	var remortport = $("#remortport").val();
	$('#myModal').modal('hide');
	loadLoginPage(remortroot, remortport);
});
	</script>
</body>
</html>